package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.AlertDataService;
import com.zzyl.nursing.vo.AlertDataVo;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author LENOV
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataVo 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataVo alertDataVo)
    {
        return alertDataMapper.selectAlertDataList(alertDataVo);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
    /**
     * 处理设备报警数据
     * @param alertDataDto
     */
    @Override
    public void lihandleAlterDatast(AlertDataDto alertDataDto) {
        AlertData alertData = this.selectAlertDataById(alertDataDto.getId());
        if(ObjectUtil.isEmpty(alertData)){
            throw new BaseException("数据不存在");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = sysUserService.selectUserById(loginUser.getUserId());
        if (ObjUtil.isEmpty(sysUser)){
            throw new BaseException("用户不存在");
        }
        alertData.setProcessorName(sysUser.getNickName());
        alertData.setProcessingResult(alertDataDto.getProcessingResult());
        alertData.setProcessingTime(alertDataDto.getProcessingTime());
        alertData.setStatus(1);
        updateById(alertData);
    }
}
